package com.huajin.codetest.xml.xstream;

import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.huajin.codetest.util.DateTimeUtil;
import com.huajin.codetest.util.HttpUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class SiteMapMain {
	
	public static void main(String[] args) throws Exception {
		List<String> urlList = new ArrayList<>();
		List<GuidData> guidList = new ArrayList<>();
		String requestUrl = "http://cwrrserver.chinawrr.local/api/data/findWarehouseDetailAll" + "?version=" + URLEncoder.encode("test", "UTF-8");
		guidList = JSONArray.parseArray(JSON.parseObject(HttpUtil.requestHttpGet(requestUrl)).getString("data"), GuidData.class);
        for (GuidData guid : guidList) {
        	urlList.add("https://www.chinawrr.com/staticData/ck/" + guid.getGuid() + ".html");
        }
        requestUrl = "http://cwrrserver.chinawrr.local/api/data/findProvideDetail" + "?version=" + URLEncoder.encode("test", "UTF-8");
        guidList = JSONArray.parseArray(JSON.parseObject(HttpUtil.requestHttpGet(requestUrl)).getString("data"), GuidData.class);
        for (GuidData guid : guidList) {
        	urlList.add("https://www.chinawrr.com/staticData/ccglqy/" + guid.getGuid() + ".html");
        }
        requestUrl = "http://cwrrserver.chinawrr.local/api/data/findPlatformDetailAll" + "?version=" + URLEncoder.encode("test", "UTF-8");
        guidList = JSONArray.parseArray(JSON.parseObject(HttpUtil.requestHttpGet(requestUrl)).getString("data"), GuidData.class);
        for (GuidData guid : guidList) {
        	urlList.add("https://www.chinawrr.com/staticData/szhxtfws/" + guid.getGuid() + ".html");
        }
        Set<Url> urlSet = new HashSet<>();
        for (String link : urlList) {
        	urlSet.add(new Url().setLoc(link).setLastmod(DateTimeUtil.localDateToDateString(LocalDate.now(), "yyyy-MM-dd")));
		}
        XStream xStream = new XStream(new DomDriver());
		//XStream xStream = new XStream(new StaxDriver());
		xStream.alias("urlSet", Set.class);
	    xStream.processAnnotations(new Class[]{Url.class});
	    //String xml = xStream.toXML(urlSet);
        xStream.toXML(urlSet, new FileOutputStream("C:\\Users\\Administrator\\Desktop\\site2.xml"));
	}
	
}
